package net.wanderinghorse.jsonmessage.response;

import net.wanderinghorse.json.JSONObject;
import net.wanderinghorse.jsonmessage.Response;
import net.wanderinghorse.jsonmessage.Request;
import net.wanderinghorse.jsonmessage.AbstractRequestHandler;

/**
 * Handler for the "ping" message type, as described in the library manual.
 */
public class Ping  extends AbstractRequestHandler {
    /**
     * Populates the response payload as described for this message type in the library manual.
     *
     * @param req The request object which caused the fetching of the response.
     * @param resp The response object to populate.
     */
    protected void doProcessResponse(final Request req, final Response resp) throws Exception {
        final long rests = req.getTimestamp();
        JSONObject pay = new JSONObject();
        pay.put( "requestTime", rests );
        pay.put( "timeDiffSeconds", (resp.getTimestamp() - rests) / 1000.0);
        pay.put( "pingPayload", req.getPayload() );
        resp.setPayload( pay );
        resp.setResultText( "Pong!" );
    }
}
